package com.longnuoqi.mydoctor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.longnuoqi.mydoctor.activity.NearbyHospitalActivity;
import com.longnuoqi.mydoctor.adapter.NearbyHospitalListViewAdapter;
import com.longnuoqi.mydoctor.entity.Hospital;
import com.longnuoqi.mydoctor.utils.FileUtil;
import com.yunhe.mydoctor.R;

import java.util.List;

/**
 * Created by Lenovo on 2016/1/5.
 */
public class NearbyHospitalFragment extends Fragment {
    private ListView listView;
    private NearbyHospitalListViewAdapter adapter;
    private double longitude;//经度
    private double latitude;//纬度
    private String url;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.nearby_lv_layout, container, false);
        longitude = getArguments().getDouble("longitude");
        latitude = getArguments().getDouble("latitude");
        url = "http://www.tngou.net/api/hospital/location?x=" + longitude + "&y=" + latitude + "&rows=8";
        initView(view);
        return view;
    }

    private void initView(View view) {
        listView = (ListView) view.findViewById(R.id.nearby_listview);
        RequestQueue queue = Volley.newRequestQueue(getActivity());
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                List<Hospital> list = FileUtil.jsonHospitalList(s);
                Log.e("================", "hospital list size:" + list.size());
                adapter = new NearbyHospitalListViewAdapter(getActivity(), list);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        int hospitalId = (int) ((view.findViewById(R.id.nearby_lv_name_txt)).getTag());
                        Intent intent = new Intent(getActivity(), NearbyHospitalActivity.class);
                        intent.putExtra("id", hospitalId);
                        startActivityForResult(intent, 0);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        queue.add(request);
    }

}
